package com.hydra.auth.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.hydra.auth.dto.DictDataQueryDto;
import com.hydra.auth.entity.DictData;
import com.hydra.auth.mapper.DictDataMapper;
import com.hydra.auth.repository.DictDataService;
import com.hydra.base.controller.BaseController;
import com.hydra.base.operate.log.dto.CommonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 系统字典表的控制器
 * @Author Hydra
 * @Date 2023/12/21 17:42
 */
@Slf4j
@RequestMapping("/api/sys/dict/data")
@RestController
public class DictDataController extends BaseController<DictDataQueryDto, DictData, DictDataMapper, DictDataService> {

    @Autowired
    public DictDataController(DictDataService repository){
        super.setRepository(repository);
    }

    @Override
    protected QueryWrapper<DictData> handleQueryWrapper(QueryWrapper<DictData> wrapper, DictDataQueryDto params) {
        wrapper.orderByAsc("sort");
        return wrapper;
    }

    /**
     * 根据字典类型查询字典
     * @param typeCode  字典类型
     */
    @GetMapping("/getDictDataByTypeCode")
    public List<DictData> getDictDataByTypeCode(@RequestParam("typeCode") String typeCode){
        return this.repository.getDictDataByTypeCode(typeCode);
    }

    /**
     * 根据字典类型查询字典
     * @param typeCode  字典类型
     */
    @GetMapping("/getDictDataMapByTypeCode")
    public Map<String, CommonData> getDictDataMapByTypeCode(@RequestParam("typeCode") String typeCode){
        List<DictData> list = this.repository.getDictDataByTypeCode(typeCode);
        if(CollectionUtil.isEmpty(list)){
            return Maps.newHashMap();
        }
        return list.stream().collect(Collectors.toMap(DictData::getCode, t->new CommonData(t.getCode(), t.getName()), (a1, a2)->a1));
    }

    /**
     * 根据字典类型、编码查询字典
     * @param typeCode  字典类型
     * @param code      字典编码
     */
    @GetMapping("/getDictDataByTypeCodeAndCode")
    public DictData getDictDataByTypeCodeAndCode(@RequestParam("typeCode") String typeCode, @RequestParam("code") String code){
        return this.repository.getDictDataByTypeCodeAndCode(typeCode, code);
    }

    /**
     * 根据类型编码和状态获取字典数据
     */
    @GetMapping("/getDictDataByTypeCodeAndStatus")
    public List<DictData> getDictDataByTypeCodeAndStatus(@RequestParam("typeCode") String typeCode, @RequestParam("status") String status){
        return this.repository.getDictDataByTypeCodeAndStatus(typeCode, status);
    }

    /**
     * 根据字典类型、数据name查询字典
     * @param typeCode  字典类型
     * @param name      数据name
     */
    @GetMapping("/getDictDataByTypeCodeAndName")
    public DictData getDictDataByTypeCodeAndName(@RequestParam("typeCode") String typeCode, @RequestParam("name") String name){
        return this.repository.getDictDataByTypeCodeAndName(typeCode, name);
    }

}
